package com.offcn.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.offcn.bean.Btype;
import com.offcn.bean.BtypeExample;
import com.offcn.mapper.BtypeMapper;
import com.offcn.service.BtypeService;
import com.offcn.utils.OAResult;
import com.offcn.utils.PageView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class BtypeServiceImpl implements BtypeService {
    @Autowired
    private BtypeMapper btypeMapper;


    public List<Btype> getBigCategory() {
        BtypeExample btypeExample=new BtypeExample();
        BtypeExample.Criteria criteria = btypeExample.createCriteria();
        criteria.andTypePidIsNull();
        List<Btype> btypeList = btypeMapper.selectByExample(btypeExample);

        for (Btype btype : btypeList) {
            BtypeExample btypeExample2=new BtypeExample();
            BtypeExample.Criteria criteria2 = btypeExample2.createCriteria();
            criteria2.andTypePidEqualTo(btype.getTypeid());
            List<Btype> subBtypeList = btypeMapper.selectByExample(btypeExample2);
            btype.setSubBtypeList(subBtypeList);
        }

        return btypeList;
    }

    public List<Btype> getSmallCategory(int typeid) {
        BtypeExample btypeExample = new BtypeExample();
        BtypeExample.Criteria criteria = btypeExample.createCriteria();
        criteria.andTypePidEqualTo(typeid);
        return btypeMapper.selectByExample(btypeExample);
    }

    public OAResult saveBtype(Btype btype) {
        int row = btypeMapper.insert(btype);
        if (row==1){
            return OAResult.ok(200,"添加成功");
        }
        return OAResult.ok(400,"添加失败");
    }

    public OAResult saveBtypeSmall(Btype btype, HttpSession session) {
        Btype btype1 = (Btype) session.getAttribute("typeid");
        if (btype1!=null){
            btype.setTypePid(btype1.getTypeid());
        }
        int row = btypeMapper.insert(btype);
        if (row==1){
            return OAResult.ok(200,"添加成功");
        }
        return OAResult.ok(400,"添加失败");
    }

    public OAResult deleteBtype(int typeid) {
        int row = btypeMapper.deleteByPrimaryKey(typeid);
        if (row==1){
            return OAResult.ok(200,"删除成功");
        }
        return OAResult.ok(400,"删除失败");
    }

    public OAResult deleteBtypeBig(int typeid) {
        BtypeExample btypeExample = new BtypeExample();
        BtypeExample.Criteria criteria = btypeExample.createCriteria();
        criteria.andTypePidEqualTo(typeid);
        btypeMapper.deleteByExample(btypeExample);
        int row = btypeMapper.deleteByPrimaryKey(typeid);
        if (row==1){
            return OAResult.ok(200,"删除成功");
        }
        return OAResult.ok(400,"删除失败");
    }

    public Btype getBtypeById(int typeid) {
        return btypeMapper.selectByPrimaryKey(typeid);
    }

    public OAResult updateBtype(Btype btype) {
        int rows = btypeMapper.updateByPrimaryKey(btype);
        System.out.println(btype.getTypeid());
        if (rows==1){
            return OAResult.ok(200,"更新成功");
        }
        return OAResult.ok(400,"更新失败");
    }


}
